package com.yy.yyui.text
{
	import com.yy.tools.string.StringUtil;
	
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class TextLabel extends TextField
	{
		public static const SIZECHANGE:String = "TEXTLABEL_SIZECHNAGE";
		private var fmt:TextFormat;
		private var _autoFit:Boolean;
		
		private var _supportUBB:Boolean;
		
		private var _data:Object;
		
		public function TextLabel(txt:String="",width:Number=100,lines:uint = 1,glow:Boolean = false)
		{
			super();
			this.textColor = 0xffffff;
			this.selectable = false;
			this.wordWrap = true;
			this._autoFit = false;
			fmt = new InnerTextFormat(this);
			this.width = width;
			this.setHeightByLine(lines);
			this.text = txt;
			this._supportUBB = false;
			if(glow)
			{
				var txtGF:GlowFilter=new GlowFilter(0x000000,1,2,2,16);
				var myGF_arr:Array=new Array();
				myGF_arr.push(txtGF);
				this.filters=myGF_arr;
			}
		}
		
	
		private static function emptyFunction(...rest):*{}
		

		
		public function update():void{
			this.text = this.text;
		}
		
		public function set supportUBB(value:Boolean):void{
			this._supportUBB = value;
			this.update();
		}
		
		public function get supportUBB():Boolean{
			return this._supportUBB;
		}
		
		override public function set width(value:Number):void{
			super.width = value;
			this.dispatchEvent(new Event(SIZECHANGE));
		}
		
		override public function set height(value:Number):void{
			super.height = value;
			this.dispatchEvent(new Event(SIZECHANGE));
		}
		
		override public function set text(value:String):void{
			var old:Object = super.text;
			if(this.supportUBB){
//				super.htmlText = StringTool.decodeUBB(value);
			}else{
				super.text = value==null?"":value;
				this.setTextFormat(fmt);
			}
			if(this.autoFit) this.adjustWidth();
			if(old!=value){
				this.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		public function set textNoNotify(value:String):void{
			var old:Object = super.text;
			if(this.supportUBB){
//				super.htmlText = StringTools.decodeUBB(value);
			}else{
				super.text = value==null?"":value;
				this.setTextFormat(fmt);
			}
			if(this.autoFit) this.adjustWidth();			
		}
		
		public function set textFormat(ft:TextFormat):void
		{
			this.setTextFormat(ft);
		}
		
		public function get textFormat():TextFormat{
			return fmt;
		}
		//设置行的高度
		public function setHeightByLine(line:int):void{
			this.height = 4+line*(uint(fmt.size)+uint(fmt.leading)+3);
		}
		
		
		public function get autoFit():Boolean{
			return this._autoFit;
		}
		public function set autoFit(value:Boolean):void{
			this._autoFit = value;
			if(value){
				this.adjustWidth();
			} 
		}
		
		public function adjustWidth():void{
			var strArr:Array = this.text.split('\r');
			var strLen:int = 0;
			var i:int,l:int;
			for(i=0,l=strArr.length;i<l;i++){
				strLen = Math.max(StringUtil.getStringLengthByCodes(strArr[i]),strLen); 
			}
			this.width = ((strLen+1)*Number(this.textFormat.size))/2;
			return;
			if(ruler==null){
				ruler = new TextField();
				ruler.autoSize = TextFieldAutoSize.LEFT;
				ruler.width = 0xffffff;
			}
			ruler.text = this.text;
			ruler.setTextFormat(this.textFormat);			
			this.width = ruler.textWidth+5;
			//this.width = GT.calcStringWidth(this.text,this.textFormat.size)+2;
		}
		
		private static var ruler:TextField;
		
        override public function get length():int
        {
            return getCharLength(text);
        }		
		
        private function getCharLength(txt:String):int
        {
        	return StringUtil.getStringLengthByCodes(txt);

        }
		
		public function get data():Object
		{
			return _data;
		}
		
		public function set data(value:Object):void
		{
			_data = value;
		}

	}

}

import com.yy.yyui.text.TextLabel;

import flash.text.TextFormat;
		
class InnerTextFormat extends TextFormat{
	private var tl:TextLabel;
	public function InnerTextFormat(tl:TextLabel){
		this.tl = tl;
		super.color = 0x000000;
		super.size = 12;
		super.leading = 0;
//		super.font = GlobalConfig.SYS_FONT;
	}
	
	override public function set color(value:Object):void{
		super.color = value;
		tl.setTextFormat(this);
	} 
	
	override public function set align(value:String):void{
		super.align = value;
		tl.setTextFormat(this);
	}
	
	
	
}